package com.yzjs.localhost.service.sysDept.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.BeanUtil;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dictionaries.NeusoftDataStatus;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.SysRoleEntityDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.sysDept.SysDeptEntity;
import com.yzjs.localhost.dao.sysDept.SysDeptMapper;
import com.yzjs.localhost.entity.sysRole.SysRoleEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftSysDept.TNeusoftSysDeptEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.sysDept.SysDeptService;
import com.yzjs.localhost.service.tNeusoftSysDept.TNeusoftSysDeptService;
import com.yzjs.localhost.service.tNeusoftSysMenu.TNeusoftSysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("sysDeptService")
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptEntity, SysDeptMapper > implements SysDeptService {


    @Autowired
    private TNeusoftSysDeptService  neusoftSysDeptService;

    @Override
    public ComparisonResultDTO DeptCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce =dto.getBeforce();
        String beforceInfo  = beforce.getInfo();
        Map<String,Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo  = after.getInfo();

        ComparisonResultDTO resultDTO = null ;
        if (beforce.getOperatorCode().contains("dept:add")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色id,备注，角色编码，角色名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"deptId,deptName,orderNum,phone");
        }else if (beforce.getOperatorCode().contains("dept:edit")){
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色id,备注，角色编码，角色名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"deptId,deptName,orderNum,phone");
        } else if (beforce.getOperatorCode().contains("dept:remove")){
            beforceParse.put("delFlag","2");
            Map<String,Object>  afterParse = JsonUtil.json2map(afterInfo);
            // 比对 角色状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(),beforceParse,afterParse,"deptId,delFlag");
            if (null==afterParse.get("deptId")){
                resultDTO.setAbstractName("主键ID为空");
                resultDTO.setWarningInfo("主键ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            SysDeptEntity deptEntity   = new SysDeptEntity();
            deptEntity.setDeptId(Long.valueOf((String) afterParse.get("deptId")));
            deptEntity.setDelFlag((String) afterParse.get("delFlag"));
            deptEntity.setDataIdentification(after.getDataIdentification());
            this.insert(deptEntity);
            return resultDTO;
        }
        // 记录表表信息
        SysDeptEntity deptEntity   = JsonUtil.json2pojo(after.getInfo(), SysDeptEntity.class);
        deptEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        deptEntity.setDataIdentification(after.getDataIdentification());
        this.insert(deptEntity);
        SynchronizationSysDept(deptEntity,beforce.getOperatorCode());
        return resultDTO;
    }

    /**
     * 同步部门信息，东软接收的部门数据进行更新本地权限
     * @param deptEntity
     * @param operatorCode
     */
    @Override
    public void SynchronizationSysDept(SysDeptEntity deptEntity,String operatorCode) {
        TNeusoftSysDeptEntity neusoftSysDept  = new TNeusoftSysDeptEntity();
        neusoftSysDept.setDeptId(deptEntity.getDeptId());
        TNeusoftSysDeptEntity resultDept = neusoftSysDeptService.findOne(neusoftSysDept);

        if(resultDept == null){
            BeanUtils.copyProperties(deptEntity,neusoftSysDept);
            neusoftSysDeptService.insert(neusoftSysDept);
        } else {
            BeanUtils.copyProperties(deptEntity,resultDept);
            neusoftSysDeptService.update(resultDept);
        }
        return;
    }

}